package com.lovo.ssc.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.ssc.entity.*;
import com.lovo.ssc.mapper.SysDeliverMapper;
import com.lovo.ssc.mapper.SysGoods2Mapper;
import com.lovo.ssc.mapper.SysGoodsMapper;
import com.lovo.ssc.mapper.SysStoreMapper;
import com.lovo.ssc.service.IBuyPlanService;
import com.lovo.ssc.service.ISysGoods2Service;
import com.lovo.sscsys.dto.BuyPlanMQDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 总库存商品表 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-07-07
 */
@Service
@Transactional
public class SysGoods2ServiceImpl extends ServiceImpl<SysGoods2Mapper, SysGoods2> implements ISysGoods2Service {
    //1总库存
    @Autowired
    SysGoods2Mapper sysGoods2Mapper;
    //2分批次库存
    @Autowired
    SysGoodsMapper sysGoodsMapper;
    //3出库记录
    @Autowired
    SysDeliverMapper sysDeliverMapper;
    //4.采购计划
    @Autowired
    IBuyPlanService buyPlanService;
    //5.入库记录
    @Autowired
    SysStoreMapper sysStoreMapper;

    /**一
     * 修改阈值
     * @param id
     * @param newScope
     */
    @Override
    public void updateScope(Integer id,Integer newScope) {
         SysGoods2 sysGoods2=  baseMapper.selectById(id);
            sysGoods2.setScope(newScope);
         baseMapper.updateById(sysGoods2);
    }

    /**二
     * 因上架出库
     * 1)减批次库存表2)减总库存表3)判断阈值,发计划
     * @param batch 批次号
     * @param num 数量
     */
    @Override
    public void outStorage(String goodsCode,String batch, int num) {
        //这里还是需要商品编号+批次号才能唯一确定上架商品！！！
        String goodsName;
         //1.减批次表库存
        QueryWrapper qw=new QueryWrapper();
        qw.eq("code",goodsCode);
        qw.eq("batch",batch);
        //查出商品
      SysGoods sysGoods =sysGoodsMapper.selectOne(qw);
      //得到对应商品编号
      goodsName=sysGoods.getGoodsName();
      sysGoods.setStockNum(sysGoods.getStockNum()-num);
      sysGoodsMapper.updateById(sysGoods);

        //2.添加批次出库记录
        SysDeliver sysDeliver=new SysDeliver();
           sysDeliver.setGoodsCode(goodsCode);
           sysDeliver.setGoodsName(goodsName);
           sysDeliver.setBatch(batch);
           sysDeliver.setNum(num);
           sysDeliver.setType(sysGoods.getType());
           sysDeliver.setSpecs(sysGoods.getSpecs());
        //时间格式转换
        DateTimeFormatter dtf =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String format = dtf.format(LocalDateTime.now());
           sysDeliver.setDeliverDate(format);
        sysDeliverMapper.insert(sysDeliver);
        //3.减总库存
        QueryWrapper qw1=new QueryWrapper();
        qw1.eq("code",goodsCode);
        SysGoods2 sysGoods2=sysGoods2Mapper.selectOne(qw1);
        sysGoods2.setStockNum(sysGoods2.getStockNum()-num);
        sysGoods2Mapper.updateById(sysGoods2);

        //4.判断阈值,按批次号查找商品号，根据商品号查找对应总库存、阈值，比较总库存与阈值，
        //如果总库存不足，则自动发送采购计划
        int stockNum=sysGoods2.getStockNum();
        int scope=sysGoods2.getScope();
        if(stockNum<scope){
        //还需要判断该商品是否已有状态为“已申报”的采购计划
            QueryWrapper qw2=new QueryWrapper();
            qw2.eq("goodsName",goodsName);
            qw2.eq("type",sysGoods.getType());
            qw2.eq("specs",sysGoods.getSpecs());
            qw2.eq("auditState","已申报");
         BuyPlan buyPlan= buyPlanService.getOne(qw2);
             if(buyPlan==null){
                 BuyPlanMQDto dto=new BuyPlanMQDto();
                 dto.setGoodsName(goodsName);
                 dto.setType(sysGoods.getType());
                 dto.setSpecs(sysGoods.getSpecs());
                 buyPlanService.addAutoPlan(dto);
             }

        }


    }

    /**三
     * 退货导致库存增加
     * @param goodsCode
     * @param batch
     * @param num
     */
    @Override
    public void inStorageCauseByReturn(String goodsCode, String batch, int num) {
        String goodsName;
        //1.加批次表库存
        QueryWrapper qw=new QueryWrapper();
        qw.eq("code",goodsCode);
        qw.eq("batch",batch);
        SysGoods sysGoods =sysGoodsMapper.selectOne(qw);
        //得到对应商品名称
        goodsName=sysGoods.getGoodsName();
        sysGoods.setStockNum(sysGoods.getStockNum()+num);
        sysGoodsMapper.updateById(sysGoods);
        //2.添加批次入库记录
        SysStore sysStore=new SysStore();
        sysStore.setGoodsCode(goodsCode);
        sysStore.setGoodsName(goodsName);
        sysStore.setBatch(batch);
        sysStore.setNum(num);
        sysStore.setOperation("退货入库");
        sysStore.setType(sysGoods.getType());
        sysStore.setSpecs(sysGoods.getSpecs());
        //时间格式转换
        DateTimeFormatter dtf =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String format = dtf.format(LocalDateTime.now());
        sysStore.setStoreDate(format);
        sysStoreMapper.insert(sysStore);
        //3.加总库存
        QueryWrapper qw1=new QueryWrapper();
        qw1.eq("code",goodsCode);
        SysGoods2 sysGoods2=sysGoods2Mapper.selectOne(qw1);
        sysGoods2.setStockNum(sysGoods2.getStockNum()+num);
        sysGoods2Mapper.updateById(sysGoods2);
    }
    /**四
     * 定价操作
     */
    @Override
    public void updatePrice(String goodsCode, Float newPrice) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("code",goodsCode);
       SysGoods2 sysGoods2= sysGoods2Mapper.selectOne(qw);
       sysGoods2.setSellPrice(newPrice);
       sysGoods2.setState("已定价");
       sysGoods2Mapper.updateById(sysGoods2);
    }


}
